remove(list=ls())

setwd("~/Harris-Public-Policy Dropbox/Christopher Blair/")

library("tmap")
library("tmaptools")
library("oldtmaptools")
library("sf")
library("geos")
library("leaflet")
library("haven")
library("raster")
library("tidyverse")
library("sp")
library("rnaturalearth")
library("rmapshaper")
library("haven")
library("leaflet")
library("rgdal")
library("ggplot2")

grayscale <- c("#E8E8E8", "#BEBEBE", "#888888", "#505050", "#101010")
bluescale <- c("#eff3ff", "#c6dbef", "#9ecae1", "#6baed6", "#3182bd", "#08519c")
greenscale <- c("#edf8e9", "#bae4b3", "#74c476", "#31a354", "#006d2c")
purpscale <- c("#f2f0f7", "#cbc9e2", "#9e9ac8", "#756bb1", "#54278f")
grayscale <- c("#f7f7f7", "#cccccc", "#969696", "#636363", "#252525")
gnbuscale <- c("#ffffcc", "#c7e9b4", "#7fcdbb", "#41b6c4", "#2c7fb8", "#253494")
orrdscale <- c("#feedde", "#fdd0a2", "#fdae6b", "#fd8d3c", "#e6550d", "#a63603")
wbluescale <- c("#ffffff", "#bdd7e7", "#6baed6", "#3182bd", "#08519c")
wgreenscale <- c("#ffffff", "#bae4b3", "#74c476", "#31a354", "#006d2c")
wpurpscale <- c("#ffffff", "#cbc9e2", "#9e9ac8", "#756bb1", "#54278f")
wgrayscale <- c("#ffffff", "#cccccc", "#969696", "#636363", "#252525")
wgnbuscale <- c("#ffffff", "#bae4bc", "#7bccc4", "#43a2ca", "#0868ac")
worrdscale <- c("#ffffff", "#fdcc8a", "#fc8d59", "#e34a33", "#b30000")

data <- read_dta("Projects/Detainee Operations/Data/new_detain.dta")

shape<- st_read("Projects/Detainee Operations/Data/GIS Boundary Files/Districts/iraq_districts.shp")
shape <- st_make_valid(shape)

qtm(shape, fill="white", borders="black")

# March 2006 Detainee Spending

data0306 <- data[ which(data$moyr==554),]

names(data0306)[names(data0306) == "district"] <- "ADM3NAME"

map <- merge(shape, data0306, by='ADM3NAME')

spend0306 <- tm_shape(map) +
  tm_borders()+
  tm_fill("pc_spent_detain", title = "Detainee Release \nPayments/1000 Pop.", palette=wgnbuscale, style="fixed",
          breaks=c(0, .0000000000001, .19,.89,2.783,7.66, 205), interval.closure="left", 
          labels=c("0", ">0-0.19","0.2-0.89", "0.9-2.78", "2.79-7.66", ">7.67"))+
  tm_layout("March 2006", title.size=1,
            legend.position = c("left","bottom"), title.position=c(.02,.96))

spend0306

# Sept. 2006 Detainee Spending

data0906 <- data[ which(data$moyr==560),]

names(data0906)[names(data0906) == "district"] <- "ADM3NAME"

map <- merge(shape, data0906, by='ADM3NAME')

spend0906 <- tm_shape(map) +
  tm_borders()+
  tm_fill("pc_spent_detain", title = "Detainee Release \nPayments/1000 Pop.", palette=wgnbuscale, style="fixed",
          breaks=c(0, .0000000000001, .19,.89,2.783,7.66, 205), interval.closure="left", 
          labels=c("0", ">0-0.19","0.2-0.89", "0.9-2.78", "2.79-7.66", ">7.67"))+
  tm_layout("September 2006", title.size=1,
            legend.position = c("left","bottom"), title.position=c(.02,.96))

spend0906

# March 2007 Detainee Spending

data0307 <- data[ which(data$moyr==566),]

names(data0307)[names(data0307) == "district"] <- "ADM3NAME"

map <- merge(shape, data0307, by='ADM3NAME')

spend0307 <- tm_shape(map) +
  tm_borders()+
  tm_fill("pc_spent_detain", title = "Detainee Release \nPayments/1000 Pop.", palette=wgnbuscale, style="fixed",
          breaks=c(0, .0000000000001, .19,.89,2.783,7.66, 205), interval.closure="left", 
          labels=c("0", ">0-0.19","0.2-0.89", "0.9-2.78", "2.79-7.66", ">7.67"))+
  tm_layout("March 2007", title.size=1,
            legend.position = c("left","bottom"), title.position=c(.02,.96))

spend0307

# Sept. 2007 Detainee Spending

data0907 <- data[ which(data$moyr==572),]

names(data0907)[names(data0907) == "district"] <- "ADM3NAME"

map <- merge(shape, data0907, by='ADM3NAME')

spend0907 <- tm_shape(map) +
  tm_borders()+
  tm_fill("pc_spent_detain", title = "Detainee Release \nPayments/1000 Pop.", palette=wgnbuscale, style="fixed",
          breaks=c(0, .0000000000001, .19,.89,2.783,7.66, 205), interval.closure="left", 
          labels=c("0", ">0-0.19","0.2-0.89", "0.9-2.78", "2.79-7.66", ">7.67"))+
  tm_layout("September 2007", title.size=1,
            legend.position = c("left","bottom"), title.position=c(.02,.96))

spend0907

# March 2008 Detainee Spending

data0308 <- data[ which(data$moyr==578),]

names(data0308)[names(data0308) == "district"] <- "ADM3NAME"

map <- merge(shape, data0308, by='ADM3NAME')

spend0308 <- tm_shape(map) +
  tm_borders()+
  tm_fill("pc_spent_detain", title = "Detainee Release \nPayments/1000 Pop.", palette=wgnbuscale, style="fixed",
          breaks=c(0, .0000000000001, .19,.89,2.783,7.66, 205), interval.closure="left", 
          labels=c("0", ">0-0.19","0.2-0.89", "0.9-2.78", "2.79-7.66", ">7.67"))+
  tm_layout("March 2008", title.size=1,
            legend.position = c("left","bottom"), title.position=c(.02,.96))

spend0308

# Sept. 2008 Detainee Spending

data0908 <- data[ which(data$moyr==584),]

names(data0908)[names(data0908) == "district"] <- "ADM3NAME"

map <- merge(shape, data0908, by='ADM3NAME')

spend0908 <- tm_shape(map) +
  tm_borders()+
  tm_fill("pc_spent_detain", title = "Detainee Release \nPayments/1000 Pop.", palette=wgnbuscale, style="fixed",
          breaks=c(0, .0000000000001, .19,.89,2.783,7.66, 205), interval.closure="left", 
          labels=c("0", ">0-0.19","0.2-0.89", "0.9-2.78", "2.79-7.66", ">7.67"))+
  tm_layout("September 2008", title.size=1,
            legend.position = c("left","bottom"), title.position=c(.02,.96))

spend0908
